<?php
class Dispatcher {
	
	private static $_instance = null;
	
	private $_defaults = array('control'=>'User', 'action'=>'defaut', 'args'=>array());
	
	private function __construct() {
		
	}
	
	public static function getInstance() {
		if(is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function dispatch($params) {
		$params = array_merge($this->_defaults, $params);
		foreach($params as $key=>$value)
			$params[$key] = !empty($params[$key]) ? $params[$key] : $this->_defaults[$key];
		
		$controller = $params['control'];
		$action = $params['action'];
		$args = $params['args'];

		$controller = Loader::loadController($controller);
		if(method_exists($controller, $action))
			$controller->$action($args);
		else {
			if(method_exists(get_parent_class($controller),$action))
				get_parent_class($controller)->$action($args);
			else
				$controller->defaut(); // execute la méthode par défaut
		}
	}
}